package webserver.core;

import webserver.http.HttpServleResponse;
import webserver.http.HttpServletRequest;

import java.io.File;
import java.net.URISyntaxException;

public class DispatcherServlet {
    //表示sources下的static目录.实际是编译后的target/classes下的static目录 。
    private static File staticDir;
    static {
        try {
            staticDir= new File(
                    ClientHandler.class.getClassLoader().getResource(
                            "./static"
                    ).toURI()
            );
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    public void service(HttpServletRequest request, HttpServleResponse response){
        String path = request.getUri();
        System.out.println("请求路径:"+path);


        File file1=new File(staticDir,path);

        if(file1.isFile()){
            response.setContentFile(file1);
        }else{
            response.setStatuseCode(404);
            response.setStatusReason("NOtFound");
            file1=new File(staticDir,"/root/404.html");
            response.setContentFile(file1);
        }
    }
}
